home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Champak 29
/
Volume 29 - JOGO DISK .iso
/
Games
/
jungle_adventure.swf
/
scripts
/
__Packages
/
GameCharacter.as
< prev
next >
Wrap
Text File
|
2006-11-29
|
26KB
|
894 lines
class GameCharacter extends SSObject
{
var radius = 22.5;
var jumpStrength = 400;
var gravity = SSGlobal.GRAVITY;
var maxWalkForce = 900;
var maxAirForce = 200;
var mass = 1;
var drag = 0.5;
var classID = SSGlobal.CLSID_MAINCHAR;
var collisionMask = 4294967295;
var assetID = "Character";
var animationLabels = ["idle","walk","airUp","airDown","hurt","death","deathInAir","end","climbHold","climbUp","climbDown","carry","carryWalk"];
var animationLabel = 0;
var active = false;
var keysLocked = false;
var sp = false;
var shieldCMax = {rb:255,gb:192,bb:0};
var depthLayer = 4;
var dispState = 0;
var dispCanTurn = true;
var dispAxis = 1;
var dispTurn = 0;
var dispTurnTime = 0.25;
var dispTurnFrames = 4;
var dispFrame = GameCharacter.prototype.dispTurnFrames;
var dispStateChanged = false;
var runTick = 0;
var eLast = 0;
var eInterval = 0.1;
var eTime = 0;
var eTimeTotal = 0;
var eLID = 0;
var canClimb = true;
var canSwing = true;
var climbLimit = false;
var corrected = false;
var inMotion = 1;
var inMotionTime = 0.25;
var canCarry = true;
var carryingTime = 0;
var jumpTime = 0;
var allowJumpTime = 0.2;
var directionX = 1;
var climbSpeed = 100;
var swinging = null;
var speed = 0;
var lastClimbTime = 0;
var multiplier = 1;
var health = 1;
var shield = 0;
var maxShield = 30;
var immune = 0;
var immuneAllowed = 3;
var immuneAtStart = 3;
var immuneBlink = 0.1;
var lockDisplayState = false;
var canLockDisplayState = true;
var finished = false;
var dead = false;
function GameCharacter()
{
super();
this.acceleration = new Vector();
this.contactNormal = new Vector();
}
function initColor()
{
if(this.colorObj)
{
var _loc2_ = this.colorObj.getTransform();
}
this.colorObj = new Color(this.target);
if(_loc2_)
{
this.colorObj.setTransform(_loc2_);
}
}
function onAddDisplay()
{
this.initColor();
this.buildAnimationData();
}
function activate()
{
this.world.viewport.setWatch(this);
this.lockDisplayState = false;
this.immune = this.immuneAtStart;
this.target._visible = true;
this.keepInScene = true;
this.controller.fuel = 0;
this.health = this.controller.health;
}
function deactivate()
{
this.toss();
this.moveTo(-300,0,0);
}
function buildAnimationData()
{
this.animationFrames = [];
var _loc2_ = this.animationLabels.length;
while((_loc2_ = _loc2_ - 1) > -1)
{
this.target.gotoAndStop(this.animationLabels[_loc2_]);
if(this.target._currentframe != 1)
{
if(this.target.a)
{
this.animationFrames[_loc2_] = Math.floor(this.target.a._totalframes * 0.5);
}
}
}
this.setAnimationState(0,true);
}
function update(elapsed)
{
if(!this.keysLocked)
{
var _loc4_ = this.controller.right - this.controller.left;
var _loc3_ = this.controller.down - this.controller.up;
var _loc5_ = this.sp != (this.sp = this.controller.jump) && this.sp;
if(this.canClimb && !this.climbing && !this.swinging && _loc3_ && this.lastClimbingObject && this.lastClimbTime + 0.5 < this.world.time)
{
if(this.x <= this.lastClimbingObject.right && this.x >= this.lastClimbingObject.left && this.y <= this.lastClimbingObject.bottom && this.y >= this.lastClimbingObject.top)
{
this.climbing = this.lastClimbingObject;
this.contactSurface = null;
}
}
}
else
{
_loc4_ = 0;
_loc3_ = 0;
var _loc0_ = null;
_loc5_ = this.sp = 0;
}
if(this.eTime)
{
this.eTime = Math.max(this.eTime - elapsed,0);
this.dropParticle();
}
if(this.shield)
{
if((this.shield -= elapsed) < 0)
{
this.onShieldEnd();
}
else
{
this.onShield();
}
}
else if(this.immune)
{
if(!(this.immune = Math.max(this.immune - elapsed,0)))
{
this.target._visible = true;
}
else
{
this.target._visible = Boolean(Math.floor(this.immune / this.immuneBlink) % 3);
}
}
this.updatePosition(elapsed,_loc4_,_loc3_,_loc5_);
if(!this.lockDisplayState)
{
this.updateDisplay(elapsed,_loc4_,_loc3_,_loc5_);
}
this.onUpdate(elapsed);
}
function onShieldEnd()
{
this.immune = this.shield = 0;
this.colorObj.setTransform({rb:0,gb:0,bb:0});
}
function onShield()
{
var _loc2_ = {};
var _loc3_ = Math.abs(Math.sin(Math.log(this.shield + 1) * 10));
_loc2_.rb = _loc3_ * this.shieldCMax.rb;
_loc2_.gb = _loc3_ * this.shieldCMax.gb;
_loc2_.bb = _loc3_ * this.shieldCMax.bb;
this.colorObj.setTransform(_loc2_);
}
function setAnimationState(state, force)
{
if(state == this.dispState && !force)
{
return undefined;
}
this.dispState = state;
this.dispTurnFrames = this.animationFrames[state];
this.target.gotoAndStop(this.animationLabels[state]);
this.target.a.gotoAndStop(this.directionX >= 0 ? this.target.a._totalframes : 1);
this.dispStateChanged = true;
}
function updateDisplay(elapsed, vX, vY, kJump)
{
if(this.swinging)
{
this.target._rotation = Math.atan2(- this.swinging.vx,this.swinging.vy) / 3.141592653589793 * 180;
this.setAnimationState(8);
}
else if(this.carrying)
{
if(this.jumpTime && vX)
{
this.setAnimationState(12);
}
else
{
this.setAnimationState(11);
}
}
else if(this.climbing)
{
if(!this.climbLimit && vY < 0)
{
this.setAnimationState(9);
}
else if(!this.climbLimit && vY > 0)
{
this.setAnimationState(10);
}
else
{
this.setAnimationState(8);
}
}
else if(this.contactSurface)
{
if(vX)
{
this.setAnimationState(1);
this.runTick += Math.max(0.005,elapsed * (this.speed / 400));
var _loc2_ = this.target.a.mcAnim;
if(this.runTick > 0.02)
{
this.runTick = 0;
if(_loc2_._currentframe == _loc2_._totalframes)
{
_loc2_.gotoAndStop(1);
}
else
{
_loc2_.nextFrame();
}
}
else
{
_loc2_.stop();
}
}
else
{
this.setAnimationState(0);
}
}
else if(!this.jumpTime)
{
if(this.velocity.y < 0)
{
this.setAnimationState(2);
}
else
{
this.setAnimationState(3);
}
}
if(this.dispCanTurn && vX && vX != this.dispAxis)
{
this.directionX = this.dispTurn = vX;
}
if(this.dispStateChanged || this.dispTurn)
{
if(this.dispTurn)
{
if(this.dispTurn > 0)
{
if((this.dispAxis = Math.min(1,this.dispAxis += elapsed * 2 / this.dispTurnTime)) == 1)
{
this.dispTurn = 0;
}
}
else if((this.dispAxis = Math.max(-1,this.dispAxis -= elapsed * 2 / this.dispTurnTime)) == -1)
{
this.dispTurn = 0;
}
}
this.dispFrame = this.dispTurnFrames + Math.round(this.dispAxis * this.dispTurnFrames) + 1;
this.target.a.gotoAndStop(this.dispFrame);
}
this.dispStateChanged = false;
}
function emitParticle(linkageID, time, options)
{
if(linkageID == null)
{
this.eTime = 0;
this.eLID = null;
return undefined;
}
this.eTime = this.eTimeTotal = time;
this.eLID = linkageID;
if(options)
{
this.eOptions = options;
}
else
{
delete this.eOptions;
}
}
function dropParticle()
{
if(this.eLast == (this.eLast = Math.floor(this.eTime / this.eInterval)))
{
return undefined;
}
var _loc2_ = new SSParticle(this.eLID,!this.eOptions.life ? 1 : this.eOptions.life,new Vector(this.velocity.x * 0.1,this.velocity.y * 0.1 - 150,-20),this.eOptions.spin,this.eOptions.gravity);
_loc2_.x = this.x;
_loc2_.y = this.y;
_loc2_.scale = 150;
this.world.addObject(_loc2_);
}
function updatePosition(elapsed, vX, vY, kJump)
{
this.corrected = false;
var _loc6_ = this.x;
var _loc5_ = this.y;
this.jumpTime = Math.max(this.jumpTime - elapsed,0);
if(this.controller.action)
{
if(this.carrying)
{
this.toss();
}
else if(this.lastContainer)
{
this.pickup(this.lastContainer);
}
}
if(this.swinging)
{
if(kJump)
{
this.releaseSwing();
}
else
{
this.swingDist += (this.swinging.length - 50 - this.swingDist) * elapsed;
this.velocity.x = (this.swinging.x + this.swinging.vx * this.swingDist - this.x) / elapsed;
this.velocity.y = (this.swinging.y + this.swinging.vy * this.swingDist - this.y) / elapsed;
}
}
else if(this.climbing)
{
if(kJump)
{
this.releaseClimb();
this.jump(elapsed,vX,vY);
this.velocity.x = this.dispAxis * 200;
this.velocity.y = -300;
}
else
{
this.climb(elapsed,vX,vY);
}
}
else if(!this.inMotion || this.contactSurface)
{
if(kJump)
{
this.jump(elapsed,vX,vY);
}
else
{
this.walk(elapsed,vX,vY);
}
}
else if(this.jumpTime && kJump)
{
this.jump(elapsed,vX,vY);
}
else
{
this.updateInSpace(elapsed,vX,vY);
}
this.contactSurface = null;
this.checkCollisions(elapsed);
this.moveBy(this.velocity.x * this.motionTime,this.velocity.y * this.motionTime,0);
if(this.carrying)
{
this.carrying.moveTo(this.x,this.top - this.carrying.radius,0);
}
if(this.jumpTime && !vX && Math.abs(_loc6_ - this.x) < 1 && Math.abs(_loc5_ - this.y) < 1)
{
if(!(this.inMotion = Math.max(this.inMotion -= elapsed,0)))
{
this.velocity.x = 0;
this.velocity.y = 20;
this.moveTo(_loc6_,_loc5_,0);
}
}
else
{
this.inMotion = this.inMotionTime;
}
}
function onSurfaceContact()
{
if(this.climbing)
{
this.releaseClimb();
}
else if(this.swinging)
{
this.releaseSwing();
}
if(!this.jumpTime)
{
GameSound.playSound(this.contactSurface.attributes.impactSound);
}
this.jumpTime = this.contactNormal.y > 0 ? 0 : this.allowJumpTime;
}
function toss()
{
if(this.carryingTime + 0.2 < this.world.time)
{
this.carrying.setVelocity(new Vector(this.velocity.x + this.dispAxis * 200,this.velocity.y - 100,0));
this.carryingTime = this.world.time;
this.carrying = null;
}
}
function pickup()
{
if(this.canCarry && !this.lastContainer || this.lastContainer.inMotion)
{
return undefined;
}
var _loc3_ = this.lastContainer.x - this.x;
var _loc2_ = this.lastContainer.y - this.y;
if(Math.sqrt(_loc3_ * _loc3_ + _loc2_ * _loc2_) < this.lastContainer.radius + this.radius)
{
this.carrying = this.lastContainer;
this.carrying.beingCarried = true;
this.carryingTime = this.world.time;
}
}
function climb(elapsed, vX, vY)
{
this.velocity.x = 0.1;
this.velocity.y = vY * this.climbSpeed;
this.x = this.climbing.x + this.climbing.localRight * 0.5;
this.lockDisplayState = false;
if(vY)
{
if(vY < 0)
{
var _loc2_ = this.climbing.top - (this.top + this.velocity.y * elapsed);
if(_loc2_ > 0)
{
this.velocity.y *= _loc2_ / this.velocity.y;
}
}
else if(this.climbing.bottom - (this.top + this.velocity.y * elapsed) < 0)
{
this.releaseClimb();
}
}
}
function startSwing(obj, dist)
{
if(!this.canSwing || this.swinging || this.climbing || obj.swingRelease + 0.5 > this.world.time)
{
return undefined;
}
if(this.carrying)
{
this.toss();
}
obj.holding = this;
this.swingDist = dist;
this.swinging = obj;
}
function releaseSwing()
{
this.velocity.x = this.swinging.vx * 300;
this.velocity.y = (this.swinging.vy - 1) * 400;
this.target._rotation = 0;
this.swinging.holding = null;
this.swinging.swingRelease = this.world.time;
this.swinging = null;
}
function releaseClimb()
{
this.lastClimbTime = this.world.time;
this.climbing = null;
}
function jump(elapsed, vX, vY)
{
var _loc2_ = new Vector(this.contactNormal.x,this.contactNormal.y - 2,0);
_loc2_.normalize();
this.velocity.x += _loc2_.x * this.jumpStrength;
this.velocity.y += _loc2_.y * this.jumpStrength;
this.jumpTime = 0;
}
function walk(elapsed, vx, vy)
{
if(!this.contactSurface)
{
return undefined;
}
var _loc5_ = this.contactSurface.attributes;
if(this.contactSurface.props & 2)
{
var _loc6_ = _loc5_.friction;
var _loc9_ = _loc5_.traction;
this.force = new Vector((- this.contactNormal.y) * vx * this.maxWalkForce * _loc9_,this.contactNormal.x * vx * this.maxWalkForce * _loc9_ + this.gravity * (!vx ? 1 : 0.25),0);
}
else
{
this.force = new Vector();
_loc6_ = 0;
_loc9_ = 0;
}
var _loc8_ = 0.004 * this.velocity.x * this.velocity.x * (this.velocity.x < 0 ? 1 : -1);
var _loc7_ = 0.004 * this.velocity.y * this.velocity.y * (this.velocity.y < 0 ? 1 : -1);
var _loc3_ = this.contactNormal.dot(this.force);
var _loc2_ = this.velocity.getNormalized();
_loc2_.x *= _loc3_ * _loc6_;
_loc2_.y *= _loc3_ * _loc6_;
this.force.x += _loc2_.x + _loc8_;
this.force.y += _loc2_.y + _loc7_;
if(!this.inMotion && vx)
{
}
this.velocity.x += this.force.x * (1 / this.mass) * elapsed;
this.velocity.y += this.force.y * (1 / this.mass) * elapsed;
}
function updateInSpace(elapsed, vX, vY)
{
this.acceleration.x = (- this.velocity.x) * this.drag + vX * this.maxAirForce;
this.acceleration.y = (- this.velocity.y) * this.drag + this.gravity;
this.velocity.x += this.acceleration.x * elapsed;
this.velocity.y += this.acceleration.y * elapsed;
}
function checkCollision(obj, flag, options)
{
var _loc4_ = undefined;
switch(obj.classID & 4294901760)
{
case SSGlobal.CLSID_MOBILEOBJECT:
case SSGlobal.CLSID_MAINCHAR:
if(SSCollision.sweepBoundsToBounds(this,obj))
{
var _loc5_ = obj.onCollision(this);
this.collisionData = null;
return _loc5_ == null ? true : _loc5_;
}
break;
case SSGlobal.CLSID_CONTAINER:
case SSGlobal.CLSID_OBJECT:
if(!this.swinging && this.controller.up && obj.classID == SSGlobal.CLSID_SWING)
{
var _loc3_ = Math.min(obj.length,Math.max(0,obj.vx * (this.x - obj.x) + obj.vy * (this.y - obj.y)));
var _loc9_ = obj.x + obj.vx * _loc3_;
var _loc8_ = obj.y + obj.vy * _loc3_;
var _loc7_ = this.x - _loc9_;
var _loc6_ = this.y - _loc8_;
var _loc10_ = Math.sqrt(_loc7_ * _loc7_ + _loc6_ * _loc6_);
if(_loc10_ < this.radius)
{
this.startSwing(obj,_loc3_);
}
break;
}
case SSGlobal.CLSID_ZONE:
if(obj.boundType)
{
if(SSCollision.sweepBoundsToBounds(this,obj))
{
_loc5_ = obj.onCollision(this);
this.collisionData = null;
return _loc5_ == null ? true : _loc5_;
}
}
else if(SSCollision.sweepSphereToSphere(this,obj,true))
{
_loc5_ = obj.onCollision(this);
this.collisionData = null;
return _loc5_ == null ? true : _loc5_;
}
break;
case SSGlobal.CLSID_SHAPE:
if(!obj.disabled && (_loc4_ = SSCollision.sweepSphereToStaticShape(this,obj)))
{
if(this.collisionData)
{
if(_loc4_.time < this.collisionData.time)
{
this.collisionData = _loc4_;
}
return true;
}
this.collisionData = _loc4_;
return true;
}
break;
case SSGlobal.CLSID_COLLECTABLE:
if(this.health && SSCollision.sweepSphereToSphere(this,obj,true))
{
obj.setCollected(this);
}
}
}
function shapeCollision(o)
{
this.speed = this.velocity.length;
var _loc4_ = undefined;
if((_loc4_ = o.edge.attributes.bounciness) == null)
{
_loc4_ = 0;
}
if(o.edge.attributes.water != null)
{
this.setDead("splash",new Vector(o.point.x - o.normal.x * this.radius,o.point.y - o.normal.y * this.radius),true);
GameSound.playSound("splash");
return undefined;
}
if(this.velocity.y > 600)
{
this.shiftHealth(-0.1);
}
var _loc6_ = this.velocity.dot(o.normal);
var _loc3_ = new Vector(_loc6_ * o.normal.x,_loc6_ * o.normal.y,0);
var _loc5_ = new Vector(this.velocity.x - _loc3_.x,this.velocity.y - _loc3_.y,0);
this.velocity.x = _loc5_.x - _loc4_ * _loc3_.x;
this.velocity.y = _loc5_.y - _loc4_ * _loc3_.y;
this.motionTime -= o.time;
this.moveTo(o.point.x + o.edge.normal.x * 0.05,o.point.y + o.edge.normal.y * 0.05,0);
this.corrected = true;
this.contactSurface = o.edge;
this.contactNormal = o.normal;
this.contactPoint = o.point;
this.onSurfaceContact();
this.checkCollisions(this.motionTime);
return true;
}
function doCommand(obj, command)
{
switch(command.toLowerCase())
{
case "loselife":
this.setDead(obj);
break;
case "end":
this.endLevel(obj);
break;
case "climb":
this.lastClimbingObject = obj;
break;
case "machine":
this.target._visibile = false;
this.removeDisplay();
this.world.references.EvilMachine.target.play();
this.update = function(elapsed)
{
};
}
}
function shiftShield(value)
{
this.target._visible = true;
this.shield = Math.min(this.shield + value,this.maxShield);
}
function shiftScore(value, obj)
{
if(!this.health)
{
return false;
}
this.controller.shiftScore(value);
return true;
}
function shiftHealth(value, obj)
{
if(value < 0 && !this.shield)
{
if(this.carrying)
{
this.toss();
}
else if(this.climbing)
{
this.releaseClimb();
}
else if(this.swinging)
{
this.releaseSwing();
}
this.lastClimbTime = this.world.time;
if(obj)
{
this.jumpTime = 0;
var _loc4_ = undefined;
var _loc2_ = undefined;
_loc4_ = new Vector(obj.x <= this.x ? 1 : -1,-1,0);
if(!(_loc2_ = obj.strength))
{
_loc2_ = 200;
}
this.velocity.x = _loc4_.x * _loc2_;
this.velocity.y = _loc4_.y * _loc2_;
this.stopCol = true;
}
}
if((this.immune || this.shield) && value < 0 || this.health <= 0)
{
return false;
}
this.controller.health = this.health = Math.max(Math.min(this.health + value,1),0);
if(this.health <= 0)
{
this.setDead(obj);
}
else if(value < 0)
{
GameSound.playSound("hurt");
this.lockDisplayState = this.canLockDisplayState;
this.immune = this.immuneAllowed;
this.hurt();
this.onSurfaceContact = function()
{
if((this.contactSurface.props & 2) == 0)
{
return undefined;
}
this.lockDisplayState = false;
delete this.onSurfaceContact;
this.onSurfaceContact();
};
}
return true;
}
function hurt()
{
this.setAnimationState(4,true);
}
function setDead(asset, point, hide)
{
if(this.dead)
{
return undefined;
}
if(this.carrying)
{
this.toss();
}
this.dead = true;
if(asset != null)
{
if(hide)
{
this.moveTo(-1000,0,0);
this.stopCol = true;
}
var _loc4_ = new SSParticle(asset,2,new Vector(0,0,0));
_loc4_.x = point.x;
_loc4_.y = point.y;
this.world.addObject(_loc4_);
}
if(this.health)
{
this.health = 0;
}
this.world.viewport.lock();
GameSound.playSound("LoseLife");
this.lockDisplayState = true;
this.keysLocked = true;
this.onSurfaceContact = function()
{
if((this.contactSurface.props & 2) == 0)
{
return undefined;
}
this.velocity.x = 0;
this.velocity.y = 1;
this.setAnimationState(5);
delete this.onSurfaceContact;
this.onSurfaceContact();
};
var _loc0_ = null;
var _loc6_ = this.world.engine.onPause = function()
{
arguments.callee.cs.pause();
arguments.callee.obj.target.a.stop();
};
var _loc0_ = null;
var _loc5_ = this.world.engine.onResume = function()
{
arguments.callee.cs.resume();
arguments.callee.obj.target.a.play();
};
_loc6_.obj = _loc5_.obj = this;
var _loc0_ = null;
var _loc0_ = null;
var _loc3_ = _loc6_.cs = _loc5_.cs = new CallStack();
if(this.contactSurface)
{
this.setAnimationState(5);
}
else
{
this.setAnimationState(6);
}
_loc3_.wait(3);
if(this.controller.lives -= 1)
{
_loc3_.call(this,this.restore);
}
else
{
_loc3_.call(this,this.endGame);
}
_loc3_.call(this,function()
{
delete this.world.engine.onResume;
delete this.world.engine.onPause;
delete this.onSurfaceContact;
}
);
_loc3_.start();
}
function endGame()
{
SSInterface.showScreen("GameOver");
}
function endLevel(obj, animated)
{
this.immune = 0;
this.target._visible = true;
GameSound.stopMusic(0.4);
this.finished = true;
this.lockDisplayState = true;
this.keysLocked = true;
var _loc2_ = new CallStack();
if(!this.contactSurface)
{
_loc2_.call(this,function()
{
return this.contactSurface != null;
}
);
}
_loc2_.call(this,function()
{
this.velocity.x *= 0.5;
this.velocity.y *= 0.5;
this.setAnimationState(7,true);
}
);
_loc2_.call(this,function()
{
SSInterface.showScreen("WinLevel",true);
}
);
_loc2_.start();
}
function setRestorePoint(obj, index)
{
this.restorePoint = obj;
}
function restore()
{
this.dead = false;
if(this.restorePoint)
{
this.moveTo(this.restorePoint.x,this.restorePoint.y - this.radius,0);
}
this.setAnimationState(2);
this.onShieldEnd();
this.velocity.loc(0,1,0);
this.controller.health = this.health = 1;
this.emitParticle(null);
this.world.viewport.unlock();
this.world.viewport.setWatch(this);
this.lockDisplayState = false;
this.keysLocked = false;
}
}